<?php

define( 'ErrorNumber.DatasourceRetrieval' , 'P-DD:0001' );
define( 'ErrorNumber.DatabaseConstruction', 'P-DD:0002' );

class ErrorStack {

	var $aErrors         = array();
	var $aStandardErrors = array();

	function ErrorStack() {
		
		// Once this gets big then move it elsewhere
		$this->aStandardErrors[ constant( 'ErrorNumber.DatasourceRetrieval'  ) ] = 'Datasource Retrieval Error';
		$this->aStandardErrors[ constant( 'ErrorNumber.DatabaseConstruction' ) ] = 'Database Construction Error';
	}

	function SetErrorHandler( &$oObject, $sFunction ) {
		set_error_handler( array( &$oObject, $sFunction ) );
	}
	
	function RestoreErrorHandler() {
		restore_error_handler();
	}

	function AddError(  $errno = null, $errstr = null, $errfile = null, $errline = null ) { // Keeping the old names for familiarity's sake
		if ( array_key_exists( $errno, $this->aStandardErrors ) ) {
			$errstr = $this->aStandardErrors[ $errno ] . ': ' . $errstr;
		}
		$this->aErrors[] = array( 'ERRNO' => $errno, 'ERRSTR' => $errstr, 'ERRFILE' => $errfile, 'ERRLINE' => $errline );
	}
	
	function HasErrors() {
		return !empty( $this->aErrors );
	}

	function GetLastError() {
		$aLastError = array_pop( $this->aErrors );
		if ( is_array( $aLastError ) ) return '[' . $aLastError['ERRNO'] . '] ' . $aLastError['ERRSTR'];
		return null;
	}

	function GetAllErrors() {
		$aErrors = array();
		while( $this->HasErrors() ) {
			$aErrors[] = $this->GetLastError();
		}
		return $aErrors;
	}

}


?>
